@file:Suppress("UNCHECKED_CAST")

package com.mofofa.maozy.extend

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.mofofa.maozy.base.BaseActivity
import com.mofofa.maozy.enums.PageTagEnum
import com.mofofa.maozy.home.HomeActivity
import com.mofofa.maozy.info.InfoActivity
import com.mofofa.maozy.info.cotediva.WalletListFcfaActivity
import com.mofofa.maozy.launch.LoginInputMobileActivity
import com.mofofa.maozy.launch.LoginInputOtpActivity
import com.mofofa.maozy.launch.PermissionActivity
import com.mofofa.maozy.loan.LoanConfirmActivity
import com.mofofa.maozy.loan.LoanLoaningActivity
import com.mofofa.maozy.loan.LoanRejectActivity
import com.mofofa.maozy.loan.LoanReviewActivity
import com.mofofa.maozy.repay.RepayActivity
import com.mofofa.maozy.utils.InfoProcessUtil
import com.mofofa.maozy.utils.UsageCountryUtil
import com.mofofa.maozy.utils.UserStateUtil

inline fun <reified T> AppCompatActivity.startMaozyToMaozyNextMaozyActivity(params: HashMap<String, String> = HashMap()) {
    try {
        val intent = Intent(this, T::class.java)
        params.keys.forEach { gkey ->
            intent.putExtra(gkey, params[gkey])
        }
        startActivity(intent)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

inline fun <reified T> AppCompatActivity.startMaozyToMaozyNextMaozyActivityMaozyAndMaozyFinish(
    params: HashMap<String, String> = HashMap()
) {
    try {
        val intent = Intent(this, T::class.java)
        params.keys.forEach { gkey ->
            intent.putExtra(gkey, params[gkey])
        }
        startActivity(intent)
        finish()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

inline fun <reified T> AppCompatActivity.startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop(
    params: HashMap<String, String> = HashMap()
) {
    try {
        val intent = Intent(this, T::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
        params.keys.forEach { gkey ->
            intent.putExtra(gkey, params[gkey])
        }
        startActivity(intent)
        finish()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun Fragment.startMaozyToMaozyNextMaozyFromMaozyPage(
    pageTag: PageTagEnum,
    params: HashMap<String, String> = HashMap()
) {
    (requireContext() as BaseActivity).startMaozyToMaozyNextMaozyFromMaozyPage(
        pageTag = pageTag,
        params = params
    )
}

fun AppCompatActivity.startMaozyToMaozyNextMaozyFromMaozyPage(
    pageTag: PageTagEnum,
    params: HashMap<String, String> = HashMap()
) {
    try {
        if (!UserStateUtil.isMaozyDidMaozyLogin()) {
            startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<LoginInputMobileActivity>(
                params
            )
            return
        }

        if (InfoProcessUtil.isMaozyRejectMaozyNow()) {
            if (this !is LoanRejectActivity) {
                startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<LoanRejectActivity>(
                    params
                )
            }
            return
        } else if (InfoProcessUtil.isMaozyRepayMaozyNow()) {
            if (this !is RepayActivity) {
                startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<RepayActivity>(
                    params
                )
            }
            return
        } else if (InfoProcessUtil.isMaozyLoaningMaozyNow()) {
            if (this !is LoanLoaningActivity) {
                startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<LoanLoaningActivity>(
                    params
                )
            }
            return
        } else if (InfoProcessUtil.isMaozyReviewingMaozyNow()) {
            if (this !is LoanReviewActivity) {
                startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<LoanReviewActivity>(
                    params
                )
            }
            return
        }

        if (pageTag == PageTagEnum.launch) {
            //判断权限
            startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<HomeActivity>(
                params
            )

        } else if (pageTag == PageTagEnum.permission) {
            startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<HomeActivity>(
                params
            )

        } else if (pageTag == PageTagEnum.login_mobile) {
            startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<HomeActivity>(
                params
            )

        } else if (pageTag == PageTagEnum.home) {
            if (InfoProcessUtil.isMaozyFinishMaozyAllMaozyPageMaozyConfig()) {
                startMaozyToMaozyNextMaozyActivity<LoanConfirmActivity>()
            } else {
                if (UsageCountryUtil.isMaozyCountryMaozyCotediva()) {
                    val curStep =
                        InfoProcessUtil.getMaozyCurrentMaozyStep()
                    if (curStep?.contains("account_info", true) == true) {
                        startMaozyToMaozyNextMaozyActivity<WalletListFcfaActivity>()
                    } else {
                        startMaozyToMaozyNextMaozyActivity<InfoActivity>()
                    }
                } else {
                    startMaozyToMaozyNextMaozyActivity<InfoActivity>()
                }
            }
        } else if (pageTag == PageTagEnum.info) {
            if (InfoProcessUtil.isMaozyFinishMaozyAllMaozyPageMaozyConfig()) {
                startMaozyToMaozyNextMaozyActivityMaozyAndMaozyFinish<LoanConfirmActivity>()
            } else {
                if (UsageCountryUtil.isMaozyCountryMaozyCotediva()) {
                    val curStep =
                        InfoProcessUtil.getMaozyCurrentMaozyStep()
                    if (curStep?.contains("account_info", true) == true) {
                        startMaozyToMaozyNextMaozyActivityMaozyAndMaozyFinish<WalletListFcfaActivity>()
                    }
                }
            }
        } else if (pageTag == PageTagEnum.apply) {
            val curStep: String? =
                InfoProcessUtil.getMaozyNextMaozyPageMaozyConfigMaozyAfterMaozyApplyMaozyPage()
            if (curStep.isNullOrBlank()) {
                startMaozyToMaozyNextMaozyActivityMaozyAndMaozyFinish<LoanConfirmActivity>()
            }
        } else if (pageTag == PageTagEnum.repay) {
            startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<HomeActivity>(
                params
            )
        } else if (pageTag == PageTagEnum.review) {
            startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<HomeActivity>(
                params
            )
        } else if (pageTag == PageTagEnum.loaning) {
            startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<HomeActivity>(
                params
            )
        } else if (pageTag == PageTagEnum.reject) {
            startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<HomeActivity>(
                params
            )
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

/** 启动权限页 */
fun AppCompatActivity.startMaozyToMaozyPermissionMaozyActivity() {
    startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<PermissionActivity>()
}

/** 启动登录页-输入手机号页 */
fun AppCompatActivity.startMaozyToMaozyLoginMaozyActivity() {
    startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<LoginInputMobileActivity>()
}

/** 启动登录页-输入手机号页 */
fun Fragment.startMaozyToMaozyLoginMaozyActivity() {
    (requireContext() as BaseActivity).startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<LoginInputMobileActivity>()
}

/** 启动登录页-输入验证码页 */
fun AppCompatActivity.startMaozyToMaozyOtpMaozyActivity() {
    startMaozyToMaozyNextMaozyActivity<LoginInputOtpActivity>()
}

/** 启动审核中页 */
fun AppCompatActivity.startMaozyToMaozyLoanMaozyReviewMaozyActivity() {
    startMaozyToMaozyNextMaozyActivityMaozyForMaozySingleMaozyTop<LoanReviewActivity>()
}